package pers.cz.service.impl;

import pers.cz.business.TaskJobBusiness;
import pers.cz.business.UserBusiness;
import pers.cz.model.PostgirlDtl;
import pers.cz.model.TaskJob;
import pers.cz.pub.BeanHelper;
import pers.cz.service.TaskJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 定时任务
 * @program: PostGirl-panent
 * @description: TaskJobSercieImpl
 * @author: Cheng Zhi
 * @create: 2021-04-26 20:34
 **/
@Service
public class TaskJobSercieImpl implements TaskJobService {

    @Autowired
    BeanHelper beanHelper;

    @Override
    public int addTask(String jobName, String cron, String testCaseIds, String ext1, String ext2, Integer userId, String testCaseType) {

        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        UserBusiness userBusiness = new UserBusiness();
        userBusiness.setMapper(beanHelper);

        PostgirlDtl postgirlDtl = userBusiness.getUserDtlByUserId(userId);
        String userName = postgirlDtl.getRealName();
        return business.addTaskJob(jobName, cron, testCaseIds, userId, userName, ext1, ext2, testCaseType);
    }

    @Override
    public List<TaskJob> queryTaskJobByJobName(String name) {

        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        List<TaskJob> taskJobs = business.queryTaskJobByJobName(name);
        return taskJobs;
    }

    @Override
    public int quertCountTaskJob() {

        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        int count = business.quertCountTaskJob();
        return count;
    }

    @Override
    public int updateTaskJobSwitch(Integer id, Boolean jobWitch) {
        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        int count = 0;
        try {
            count = business.updateTaskJobSwitch(id,jobWitch);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int updateTaskJobCron(Integer id, String jobCron, String jobName) {
        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        int count = 0;
        try {
            count = business.updateTaskJobCron(id, jobCron, jobName );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int deleteJob(Integer id) {

        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        int count = 0;
        try {
            count = business.deleteTaskJob(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public int startJobNow(Integer id) {
        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        int count = 0;
        try {
            count = business.startJobNow(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return count;
    }

    @Override
    public List<TaskJob> queryAllTaskJobByPage(Integer pageStart, Integer pageLimit) {
        TaskJobBusiness business = new TaskJobBusiness();
        business.setMapper(beanHelper);
        List<TaskJob> taskJobs = business.queryAllTaskJobByPage(pageStart, pageLimit);
        return taskJobs;
    }
}
